﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HPITRFID;

namespace StudentManage
{
    public partial class Frm_CanShu : Form
    {
        //打开串口
        [DllImport("HPITRFID/EPCSDK.dll")]
        public static extern IntPtr OpenComm(int portNo);
        //关闭串口
        [DllImport("HPITRFID/EPCSDK.dll")]
        public static extern void CloseComm(IntPtr hCom);

        //定义串口对象
        IntPtr handle = IntPtr.Zero;

        //定义串口打开状态的属性
        public bool HandleStatus
        {
            get// 获取，读取
            {
                return (int)handle != -1 && handle != IntPtr.Zero ? true : false;
            }
        }

        public Frm_CanShu()
        {
            InitializeComponent();
        }

        private void btn_Open_Click(object sender, EventArgs e)
        {
            if (HandleStatus)
            {
                MessageBox.Show("该串口已经打开");
            }
            else
            {

                int portNo = int.Parse(this.cmb_Conn.SelectedItem.ToString().Replace("COM", ""));
                handle = OpenComm(portNo);
                MessageBox.Show("打开成功");
            }
        }

        private void btn_Read_Click(object sender, EventArgs e)
        {
            try
            {
                //定义一个接收工作模式的数组
                byte[] data = new byte[1];
                //读取工作模式
                if (EPCSDKHelper.GetReaderParameters(handle, 0x70, 1, data, 0))
                {
                    this.cmb_Model.SelectedIndex = data[0] - 1;

                    //获取读卡的读卡方式
                    if (EPCSDKHelper.GetReaderParameters(handle, 0x87, 1, data, 0))
                    {
                        this.cmb_ReadModtity.SelectedIndex = data[0];

                        //获取相邻判别参数
                        if (EPCSDKHelper.GetReaderParameters(handle, 0x7B, 1, data, 0))
                        {
                            this.comboBox4.SelectedIndex = data[0] - 1;

                            //获取相邻判别时间
                            if (EPCSDKHelper.GetReaderParameters(handle, 0x7A, 1, data, 0))
                                this.txt_Miao.Text = data[0].ToString();
                            else
                                MessageBox.Show("相邻判别时间读取失败");
                        }
                        else
                            MessageBox.Show("相邻判别读取失败");
                    }
                    else
                        MessageBox.Show("读卡方式读取失败");
                }
                else
                    MessageBox.Show("工作模式读取失败");
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void btn_SheZhi_Click(object sender, EventArgs e)
        {
            try
            {
                byte[] data = new byte[1];

                //设置工作模式
                data[0] = (byte)(this.cmb_Model.SelectedIndex + 1);
                if (EPCSDKHelper.SetReaderParameters(handle, 0x70, 1, data, 0))
                {
                    //设置读卡的读卡方式
                    data[0] = (byte)(this.cmb_ReadModtity.SelectedIndex);
                    if (EPCSDKHelper.SetReaderParameters(handle, 0x87, 1, data, 0))
                    {
                        //设置相邻判别参数
                        data[0] = byte.Parse((this.comboBox4.SelectedIndex + 1).ToString());
                        if (EPCSDKHelper.SetReaderParameters(handle, 0x7B, 1, data, 0))
                        {

                            //设置相邻判别时间
                            data[0] = byte.Parse(this.txt_Miao.Text);
                            if (EPCSDKHelper.SetReaderParameters(handle, 0x7A, 1, data, 0) == false)
                                MessageBox.Show("设置相邻判别时间失败");
                        }
                        else
                            MessageBox.Show("设置相邻判别参数失败");
                    }
                    else
                        MessageBox.Show("设置读卡方式失败");
                }
                else
                    MessageBox.Show("设置工作模式失败");
            }

            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        private void btn_Close_Click(object sender, EventArgs e)
        {
            if (HandleStatus)
            {
                EPCSDKHelper.CloseComm(handle);
                handle = IntPtr.Zero;
                MessageBox.Show("关闭成功");
            }
            else
            {
                MessageBox.Show("该串口已经关闭");
            }
        }
    }
}
